package com.weiyin.examination.adapter;

import android.app.Activity;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.weiyin.examination.R;
import com.weiyin.examination.module.CourseAnswerModule;
import com.weiyin.examination.util.LayoutHelper;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author njf
 * @date 2017/11/13
 */

public class SelectTopicAdapter extends BaseQuickAdapter<CourseAnswerModule, BaseViewHolder> {
    private View oldView;
    private Activity activity;

    public SelectTopicAdapter(Activity activity, @LayoutRes int layoutResId, @Nullable List<CourseAnswerModule> data) {
        super(layoutResId, data);
        this.activity = activity;
    }

    @Override
    protected BaseViewHolder createBaseViewHolder(View view) {
        LayoutHelper layoutHelper = new LayoutHelper(activity);
        layoutHelper.scaleView(view);
        return super.createBaseViewHolder(view);
    }

    private OnItemClickInterface onItemClickInterface;

    public void setOnItemClickInterface(OnItemClickInterface onItemClickInterface) {
        this.onItemClickInterface = onItemClickInterface;
    }

    public interface OnItemClickInterface {
        /**
         * item点击事件
         *
         * @param baseViewHolder holder
         * @param position       position
         */
        void onItemClick(BaseViewHolder baseViewHolder, int position);
    }

    @Override
    protected void convert(final BaseViewHolder helper, CourseAnswerModule item) {
        if (helper != null) {
            if (!StringUtils.isEmpty(item.getString())) {
                helper.setText(R.id.selectDialogItemTextView, "第" + item.getString() + "题");
                ((TextView) (helper.getView(R.id.selectDialogItemTextView))).
                        setTextColor(activity.getResources().getColorStateList(item.isResult() ? R.color.text_select_bg_f : R.color.text_select_bg));
                helper.getView(R.id.selectDialogItemLayout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (oldView != null) {
                            oldView.setSelected(false);
                        }
                        v.setSelected(true);
                        oldView = v;
                        if (onItemClickInterface != null) {
                            onItemClickInterface.onItemClick(helper, helper.getAdapterPosition());
                        }
                    }
                });
            }
        }
    }
}
